﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;

public partial class TeacherEval_School_ScoreInput_HeaderOverView : ScoreInputBasePage
{
    protected int evalId = 0;
    private int evalType = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Int32.TryParse(Request["id"], out evalId))
                evalId = 0;
            if (!Int32.TryParse(Request["evaltype"], out evalType))
                evalType = 0;

            //InitDisplay();
            InitGradeSelect();
          //  InitSubjectSelect();

            groupChanged(selGrade as object, new EventArgs());
        }
    }

    /// <summary>
    /// 初始化年级选择下拉框
    /// </summary>
    private void InitGradeSelect()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        IList<SchoolInfo> schoolList = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        selGrade.Items.Add(new ListItem("全部", "0"));
        switch (schoolList[0].SchoolOpt)
        {
            case 1:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                break;
            case 2:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 3:
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
            case 4:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 5:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
        }
    }

    ///// <summary>
    ///// 初始化科目选择下拉框
    ///// </summary>
    //private void InitSubjectSelect()
    //{
    //    selSubject.Items.Add(new ListItem("全部", "0"));
    //    IList<GlobalCauseInfo> causeList = Container<IGlobalCauseInfoLogic>.Instance.SelectSchoolCause(SchoolMark, 1);
    //    if (causeList == null || causeList.Count < 1)
    //        return;

    //    foreach (GlobalCauseInfo info in causeList)
    //        selSubject.Items.Add(new ListItem(info.CauseName, info.RID.ToString()));
    //}

    protected void groupChanged(object sender, EventArgs e)
    {
        int subjectId = -1;
        int gradeId = -1;
        if (!Int32.TryParse(selGrade.SelectedValue, out gradeId))
            return;

       
        List<HeaderInfo> list = null;
        ITeacherInfoLogic teachLogic = Container<ITeacherInfoLogic>.Instance;
        list = (List<HeaderInfo>)teachLogic.FindHeaderByGrade(gradeId, SchoolMark);
 
        if (list == null || list.Count < 1)
            return;

        List<HeaderInfo> tt = new List<HeaderInfo>();
        List<int> teacherIdList = new List<int>();
        foreach (HeaderInfo i in list)
        {
            if (!teacherIdList.Contains(i.RID))
            {
                teacherIdList.Add(i.RID);
                tt.Add(i);
            }
            else
            {
                foreach (HeaderInfo info in tt)
                {
                    if (info.RID == i.RID)
                    {
                        info.ClsName += String.Format("<br/>{0}", i.ClsName);
                        break;
                    }
                }
                
            }
        }

        int managerId = Common.GetUserInfo().UID;
        if (evalId == 0)
        {
            string s = txtHistoryId.Text;
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out evalId);
        }
        IList<TeacherEvalState> stateList =
        Container<ITeacherEvalHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                       SchoolCommon.Sch_SchoolInfo.SchoolTerm,
                                                       SchoolMark, teacherIdList, evalId, managerId);

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", SchoolCommon.Sch_SchoolInfo.SchoolYear);
        ht.Add("Term", SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;

        CreateTable(tt, stateList, termStart);
    }

    /// <summary>
    /// 构造教师评价表格
    /// </summary>
    private void CreateTable(List<HeaderInfo> teacherList, IList<TeacherEvalState> stateList, DateTime termStart)
    {
        int width = 300;
        TableHeaderRow hrow = new TableHeaderRow();

        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班主任";
        hcell.Style[HtmlTextWriterStyle.Width] = "150px";
        hrow.Cells.Add(hcell);

        hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "150px";
        hrow.Cells.Add(hcell);


        IList<DateTime> dateList = new List<DateTime>();
        ListSort<TeacherEvalState> sort = new ListSort<TeacherEvalState>(stateList, "StartTime", false);
        stateList = sort.Sort();
 
        while (!(termStart.Month > DateTime.Now.Month && termStart > DateTime.Now))
        {

            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}月", termStart.Month);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            dateList.Add(termStart);
            termStart = termStart.AddMonths(1);
        }
        tbResult.Rows.Add(hrow);

        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (HeaderInfo info in teacherList)
        {
            row = new TableRow();

            cell = new TableCell();
            cell.Text = String.Format("{0}({1})", info.TeachName, info.TeachCode);
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = String.Format("{0}", info.ClsName);
            row.Cells.Add(cell);

            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (TeacherEvalState state in stateList)
                {
                    if (state.TeacherID == info.RID && state.StartTime.Month == dt.Month)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        //control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
                        //                        year, term,
                        //                        state.ClassID, state.StartTime.Year, state.StartTime.Month);

                        control.NavigateUrl = String.Format("../ScoreInput/TeachInput.aspx?HistoryId={0}&TeacherId={1}&TeacherName={2}&TeacherCode={3}&Year={4}&Month={5}&Type=Edit",
                            evalId, info.RID, info.TeachName, Server.UrlEncode(info.TeachCode), state.StartTime.Year, state.StartTime.Month);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    //control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
                    //                        year, term,
                    //                        info.RID, dt.Year, dt.Month);
                    
                    control.NavigateUrl = String.Format("../ScoreInput/TeachInput.aspx?HistoryId={0}&TeacherId={1}&TeacherName={2}&TeacherCode={3}&Year={4}&Month={5}&Type=Edit",
                             evalId, info.RID, Server.UrlEncode(info.TeachName), info.TeachCode, dt.Year, dt.Month);
                    cell.Controls.Add(control);
                }

                row.Cells.Add(cell);
            }

            tbResult.Rows.Add(row);
        }
    }
}
